//
//  TTHeadView.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/23.
//  Copyright © 2016年 陶路路. All rights reserved.
//

/*
addSubview(headImageView)
addSubview(nameLabel)
addSubview(memberImageView)
addSubview(timeLabel)
addSubview(sourceLabel)
addSubview(avatarImageView)

*/

import UIKit

let df = NSDateFormatter()

class TTHeadView: UIView {
    
    var homeCellModel:TTHomeCellModel?{
        
        didSet{
            
            headImageView.sd_setImageWithURL(NSURL(string: (homeCellModel?.cellModel?.user?.profile_image_url) ?? ""))
            nameLabel.text = homeCellModel?.cellModel?.user?.name
            memberImageView.image = homeCellModel?.avatarImageView
            avatarImageView.image = homeCellModel?.memberImageView
            
//            print(getSinaTime(homeCellModel?.cellModel?.created_at))
// mark : 给textLable 赋值 富文本
            sourceLabel.attributedText = homeCellModel?.souce
// mark: 时间赋值
            timeLabel.text = getSinaTime(homeCellModel?.cellModel?.created_at)
        }
    }

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
        setUpLayout()
//        self.backgroundColor = RandomColor()
    }
    
    func setUpUI(){

        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)

    }
    
    func setUpLayout(){
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        // 02
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        // 03
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        // 04
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        // 05
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        // 06
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(headImageView).offset(10)
        }

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    
    lazy var headImageView:UIImageView = {
        let head = UIImageView()
        head.image = UIImage(named: "avatar_default")
        return head
    }()
    
    lazy var nameLabel:UILabel = {
       let lable = UILabel()
        lable.textColor = UIColor.blackColor()
        lable.font = UIFont.systemFontOfSize(15)
        return lable
    }()
    
    lazy var memberImageView:UIImageView = {
        let member = UIImageView()
        member.image = UIImage(named: "common_icon_membership")
        return member
    }()
    
    lazy var timeLabel:UILabel = {
        let time = UILabel()
        time.textColor = UIColor.orangeColor()
        time.font = UIFont.systemFontOfSize(10)
        return time
    }()
    
    lazy var sourceLabel:UILabel = {
       let source = UILabel()
        source.textColor = UIColor.orangeColor()
        source.font = UIFont.systemFontOfSize(10)
        return source
    }()
    
    lazy var avatarImageView:UIImageView = {
       let avatar = UIImageView()
        avatar.image = UIImage(named: "avatar_vgirl")
        return avatar
    }()
}


// mark : 获取时间

//Optional("Tue Jul 26 19:30:03 +0800 2016")
extension TTHeadView{
    
    func getSinaTime(data:String?)->String{
        
        df.locale = NSLocale(localeIdentifier: "en_US")
        
        df.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        
        let temp = df.dateFromString(data!)
        
//        let temp = temp1?.dateByAddingTimeInterval(-66666666)
        if isThisYear(temp!){
//    判断是不是今天
           let calender = NSCalendar.currentCalendar()
            
            if calender.isDateInToday(temp!){
//    判断具体时间
               let s = Int(NSDate().timeIntervalSinceDate(temp!))
                
                if s < 60{
                    
                    return "刚刚"
                    
                }else if s >= 60 && s < 3600{
                    
                    return "\(s/60)分钟前"
                    
                }else if s > 3600 && s < 3600 * 24{
                    
                    return "\(s/3600)小时前"
                }
                
            }else if calender.isDateInYesterday(temp!){
                
                df.dateFormat = "昨天:HH:mm"
                
            }else{
                
                df.dateFormat = "MM月dd日 HH:mm"
            }
            
        }else{
            
           df.dateFormat = "yyyy年MM月dd日 HH:mm"
        }
        
        return df.stringFromDate(temp!)
    }
    
// mark 判断是不是今年
    
    func isThisYear(yearDate:NSDate)->Bool{
        
        df.dateFormat = "yyyy"
        
        let currentYear = df.stringFromDate(NSDate())
        let weiboYear = df.stringFromDate(yearDate)
        return currentYear == weiboYear
    }

}

